package com.kehao.newcoder;

public class NCSolution62 {
    private class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
    }

    public boolean IsBalanced_Solution(TreeNode root) {
        int depth = getDepth(root);
        return depth!=-1;
    }

    private int getDepth(TreeNode node){
        if(node==null) return 0;
        int depthLeft = getDepth(node.left);
        if(depthLeft == -1) return -1;
        int depthRight = getDepth(node.right);
        if(depthRight == -1) return -1;
        int dis = depthRight-depthLeft;
        if(dis>1||dis<-1){
            return -1;
        }
        return dis==1?depthRight:depthLeft;
    }
}
